package com.macro.mall.portal.controller.ums;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.model.UmsMember;
import com.macro.mall.portal.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * class
 *
 * @author xuyanjun
 * @date 2020-01-10
 */
@Api(tags = "ShareController", description = "分享管理")
@Slf4j(topic = "ShareController")
@Controller
@RequestMapping("/share")
public class ShareController extends BaseController {

    @ApiOperation("获取分享二维码")
    @GetMapping("/getShareQrCode")
    public void getShareQrCode(HttpServletResponse response) throws IOException {
        UmsMember currentUser = getCurrentUser();

        String shareUrl = "http://api-test.fulecat.com/wxMpAuth/index?parentId=" + currentUser.getId();

        BufferedImage bufferedImage = QrCodeUtil.generate(shareUrl, 200, 200);

        response.setContentType("image/png");
        ServletOutputStream outputStream = response.getOutputStream();

        ImageIO.write(bufferedImage, "png", outputStream);

        outputStream.flush();
        outputStream.close();
    }

    @ApiOperation("获取分享链接")
    @GetMapping("/getShareUrl")
    public CommonResult<String> getShareUrl(HttpServletResponse response) throws IOException {
        UmsMember currentUser = getCurrentUser();

        String shareUrl = "http://api-test.fulecat.com/wxMpAuth/index?parentId=" + currentUser.getId();
        return CommonResult.success(shareUrl);
    }
}
